/**
 * 过滤器
 * 基于oacore,jquery-treeview.js,treeview.js,jquery-form.js
 */
;(function () {
	/**
	 * onsubmit: 提交后的回调函数,可以设置一个参数以接收序列化后的数据.
	 * data: 筛选器的内容.
	 * multi: 是否多选
	 * parent: 父元素
	 */
	function Filter(params) {
		this.box = new oacore.UI.FloatBox();
		this.parent = $(params.parent);
		var host = this;
		this.data = params.data;
		this.isShow = false;
		this.multi = params.multi;
		host.parent.bind("click", function () {
			if(host.isShow)
			{
				host.box.destroy();
				return false;
			}
			var uiUrl = "dialog/radio_box.html";
			if(params.multi)
				uiUrl = "dialog/multi_select_box.html";
			oacore.UI.render(uiUrl, host.data, host.box.content, function () {
				host.isShow = true;
				host.box.content.appendTo(document.body).hide();
				host.box.content.addClass("filter-dialog");
				host.box.autoDestroy(true);
				host.box.onDestroy = function () {
					host.isShow = false;
				};
				if(host.isAllSelected())
					host.box.content.find("input.filter-all-options:first").attr("checked", "checked");
				host.box.content.find(".filter-dialog-submit:first").bind("click", function () {
					if(params.onsubmit)
					{
						var serialized = host.box.content.children("form:first").formSerialize();
						host.setDataFromParams(serialized);
						params.onsubmit(serialized);
					}
					host.box.destroy();
					return false;
				});
				host.box.content.find(".filter-options").bind("click", function () {
					if(!$(this).attr("checked"))
						host.box.content.find(".filter-all-options:first").attr("checked", "");
				});
				host.box.content.find(".filter-all-options:first").bind("click", function () {
					var $this = $(this);
					if($this.attr("checked"))
						host.box.content.find(".filter-options").each(function () {
							$(this).attr("checked", "checked");
						});
					else
					{
						host.box.content.find(".filter-options").each(function () {
							$(this).attr("checked", "");
						});
					}
				});
				var offset = host.parent.offset();
				offset.top = offset.top + host.parent.outerHeight() + 2;
				host.box.content.show().offset(offset);
			});
			return false;
		});
	};
	
	Filter.prototype.getValue = function () {
		var selected = this.getSelected();
		for(var key in selected)
			return selected[key];
		return null;
	};
	
	Filter.prototype.getSelectedData = function () {
		for(var j=0;j<this.data.length;++j)
			if(this.data[j].selected)
				return this.data[j];
	};
	
	Filter.prototype.reset = function () {
		//this.cleanSelected();
		//this.box.content.find("input.filter-select").attr("checked", "").filter("[type='radio']").attr("checked", "checked");
	};
	
	Filter.prototype.getSelected = function () {
		for(var j=0;j<this.data.length;++j)
			if(this.data[j].selected)
			{
				var param = {};
				param[this.data[j].paramName] = this.data[j].id;
				return param;
			}
	};
	
	Filter.prototype.setDataFromParams = function (params) {
		var paramsArr = params.split("&");
		this.cleanSelected();
		for(var i=0;i<paramsArr.length;++i)
		{
			var kv = paramsArr[i].split("=");
			for(var j=0;j<this.data.length;++j)
			{
				if(kv[0] == this.data[j].paramName && kv[1] == this.data[j].id)
					this.data[j].selected = true;
			}
		}
	};
	
	Filter.prototype.cleanSelected = function () {
		for(var j=0;j<this.data.length;++j)
			delete this.data[j].selected;
	};
	
	Filter.prototype.isAllSelected = function () {
		for(var j=0;j<this.data.length;++j)
			if(!this.data[j].selected)
				return false;
		return true;
	};
	
	oacore.Widget.Filter = Filter;
})();